package com.qigao.mall.web.controller.app.user;

import com.qigao.mall.api.AccountInfoService;
import com.qigao.mall.commons.dto.AccountInfoResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.utils.RequestUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author luiz
 * @Title: com.qigao.mall.web.controller.app.merchant
 * @ProjectName three-mall
 * @date 2020-04-2020/4/13 9:58
 * @Description:账户操作相关
 */
@RestController
@RequestMapping("/app/user/account/")
@Api(tags = "APP账户余额相关")
public class AppAccountController {
    private static Logger logger= LoggerFactory.getLogger(AppAccountController.class);
    //获取当前账户余额
    @Resource
    AccountInfoService accountInfoService;

    @PostMapping("/findAccount")
    @ApiOperation(value = "获取当前账户余额", notes = "获取当前账户余额")
    public Result<AccountInfoResultDto> findAccount(HttpServletRequest request){
        try{
            //参数校验
            String userNo=RequestUtils.getCurrentUserNo(request);
            return  accountInfoService.findAccountByUserNo(userNo);
        }catch (Exception e){
            logger.error("[获取当前账户余额]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @PostMapping("/findScoreAccount")
    @ApiOperation(value = "获取当前积分账户", notes = "获取当前积分账户")
    public Result<AccountInfoResultDto> findScoreAccount(HttpServletRequest request){
        try{
            //参数校验
            String userNo=RequestUtils.getCurrentUserNo(request);
            return  accountInfoService.findScoreAccount(userNo);
        }catch (Exception e){
            logger.error("[获取当前积分账户]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @PostMapping("/findAccountByType/{accountType}")
    @ApiOperation(value = "按照账户类型获取当前用户账户信息", notes = "按照账户类型获取当前用户账户信息")
    public Result<AccountInfoResultDto> findAccountByType(@PathVariable("accountType")String accountType,HttpServletRequest request){
        try{
            //参数校验
            String userNo=RequestUtils.getCurrentUserNo(request);
            return  accountInfoService.findAccountByType(userNo,accountType);
        }catch (Exception e){
            logger.error("[按照账户类型获取当前用户账户信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
